/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.battle;


import cz.fi.muni.diplomka.common.Army;
import cz.fi.muni.diplomka.common.Squad;
import cz.fi.muni.diplomka.common.Unit;
import cz.muni.fi.diplomka.servicelayer.gamelogic.resolver.BattleResolver;
import cz.muni.fi.diplomka.servicelayer.gamelogic.resolver.BattleResolver;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.UnitConvertor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class BattlePage extends WebPage {

    private BattleResolver battleResolver = new BattleResolver();
    private UnitConvertor unitConvertor = new UnitConvertor();
    // single list choice
    private static final List<String> UNITS = Arrays.asList(new String[]{
        "Axeman", "Pikeman", "Light Infantry", "Heavy Infantry", "Halberdier",
        "Mounted Pikeman", "Light Cavalry", "Heavy Cavalry", "Bowman", "Crossbowman", "Wagon"});
    private String selectedUnit1 = "Axeman"; // Axeman is selected by default
    private String selectedUnit2 = "Axeman";
    private String selectedUnit3 = "Axeman";
    private String selectedUnit4 = "Axeman";
    private String selectedUnit5 = "Axeman";
    private String selectedUnit6 = "Axeman";
    private String selectedUnit7 = "Axeman";
    private String selectedUnit8 = "Axeman";
    private String selectedUnit9 = "Axeman";
    private String selectedUnit10 = "Axeman";

    public BattlePage() {
        add(new FeedbackPanel("feedback"));


        final TextField<String> unitCount1 = new TextField<>("count1",
                Model.of("0"));
        unitCount1.add(new UnitCountValidator());

        final TextField<String> unitCount2 = new TextField<>("count2",
                Model.of("0"));
        unitCount2.add(new UnitCountValidator());

        final TextField<String> unitCount3 = new TextField<>("count3",
                Model.of("0"));
        unitCount3.add(new UnitCountValidator());

        final TextField<String> unitCount4 = new TextField<>("count4",
                Model.of("0"));
        unitCount4.add(new UnitCountValidator());

        final TextField<String> unitCount5 = new TextField<>("count5",
                Model.of("0"));
        unitCount5.add(new UnitCountValidator());

        final TextField<String> unitCount6 = new TextField<>("count6",
                Model.of("0"));
        unitCount6.add(new UnitCountValidator());

        final TextField<String> unitCount7 = new TextField<>("count7",
                Model.of("0"));
        unitCount7.add(new UnitCountValidator());

        final TextField<String> unitCount8 = new TextField<>("count8",
                Model.of("0"));
        unitCount8.add(new UnitCountValidator());

        final TextField<String> unitCount9 = new TextField<>("count9",
                Model.of("0"));
        unitCount9.add(new UnitCountValidator());

        final TextField<String> unitCount10 = new TextField<>("count10",
                Model.of("0"));
        unitCount10.add(new UnitCountValidator());

        final TextField<String> threshold1 = new TextField<>("threshold1",
                Model.of("20"));
        threshold1.add(new ThresholdValidator());

        final TextField<String> threshold2 = new TextField<>("threshold2",
                Model.of("20"));
        threshold2.add(new ThresholdValidator());

        final TextField<String> threshold3 = new TextField<>("threshold3",
                Model.of("20"));
        threshold3.add(new ThresholdValidator());

        final TextField<String> threshold4 = new TextField<>("threshold4",
                Model.of("20"));
        threshold4.add(new ThresholdValidator());

        final TextField<String> threshold5 = new TextField<>("threshold5",
                Model.of("20"));
        threshold5.add(new ThresholdValidator());

        final TextField<String> threshold6 = new TextField<>("threshold6",
                Model.of("20"));
        threshold6.add(new ThresholdValidator());

        final TextField<String> threshold7 = new TextField<>("threshold7",
                Model.of("20"));
        threshold7.add(new ThresholdValidator());

        final TextField<String> threshold8 = new TextField<>("threshold8",
                Model.of("20"));
        threshold8.add(new ThresholdValidator());

        final TextField<String> threshold9 = new TextField<>("threshold9",
                Model.of("20"));
        threshold9.add(new ThresholdValidator());

        final TextField<String> threshold10 = new TextField<>("threshold10",
                Model.of("20"));
        threshold10.add(new ThresholdValidator());

        DropDownChoice<String> listUnits1 = new DropDownChoice<>("unit1",
                new PropertyModel<String>(this, "selectedUnit1"), UNITS);

        DropDownChoice<String> listUnits2 = new DropDownChoice<>("unit2",
                new PropertyModel<String>(this, "selectedUnit2"), UNITS);

        DropDownChoice<String> listUnits3 = new DropDownChoice<>("unit3",
                new PropertyModel<String>(this, "selectedUnit3"), UNITS);

        DropDownChoice<String> listUnits4 = new DropDownChoice<>("unit4",
                new PropertyModel<String>(this, "selectedUnit4"), UNITS);

        DropDownChoice<String> listUnits5 = new DropDownChoice<>("unit5",
                new PropertyModel<String>(this, "selectedUnit5"), UNITS);

        DropDownChoice<String> listUnits6 = new DropDownChoice<>("unit6",
                new PropertyModel<String>(this, "selectedUnit6"), UNITS);

        DropDownChoice<String> listUnits7 = new DropDownChoice<>("unit7",
                new PropertyModel<String>(this, "selectedUnit7"), UNITS);

        DropDownChoice<String> listUnits8 = new DropDownChoice<>("unit8",
                new PropertyModel<String>(this, "selectedUnit8"), UNITS);

        DropDownChoice<String> listUnits9 = new DropDownChoice<>("unit9",
                new PropertyModel<String>(this, "selectedUnit9"), UNITS);

        DropDownChoice<String> listUnits10 = new DropDownChoice<>("unit10",
                new PropertyModel<String>(this, "selectedUnit10"), UNITS);

        Form<?> form = new Form<Void>("form") {
            @Override
            protected void onSubmit() {

                Army army1 = new Army();
                Army army2 = new Army();

                Unit unit1 = unitConvertor.getUnitFromXML(selectedUnit1);
                Unit unit2 = unitConvertor.getUnitFromXML(selectedUnit2);
                Unit unit3 = unitConvertor.getUnitFromXML(selectedUnit3);
                Unit unit4 = unitConvertor.getUnitFromXML(selectedUnit4);
                Unit unit5 = unitConvertor.getUnitFromXML(selectedUnit5);
                Unit unit6 = unitConvertor.getUnitFromXML(selectedUnit6);
                Unit unit7 = unitConvertor.getUnitFromXML(selectedUnit7);
                Unit unit8 = unitConvertor.getUnitFromXML(selectedUnit8);
                Unit unit9 = unitConvertor.getUnitFromXML(selectedUnit9);
                Unit unit10 = unitConvertor.getUnitFromXML(selectedUnit10);

              /*  Squad squad1 = new Squad(1, unit1, Integer.parseInt(unitCount1.getValue()), Integer.parseInt(threshold1.getValue()));
                Squad squad2 = new Squad(2, unit2, Integer.parseInt(unitCount2.getValue()), Integer.parseInt(threshold2.getValue()));
                Squad squad3 = new Squad(3, unit3, Integer.parseInt(unitCount3.getValue()), Integer.parseInt(threshold3.getValue()));
                Squad squad4 = new Squad(4, unit4, Integer.parseInt(unitCount4.getValue()), Integer.parseInt(threshold4.getValue()));
                Squad squad5 = new Squad(5, unit5, Integer.parseInt(unitCount5.getValue()), Integer.parseInt(threshold5.getValue()));

                Squad squad6 = new Squad(1, unit6, Integer.parseInt(unitCount6.getValue()), Integer.parseInt(threshold6.getValue()));
                Squad squad7 = new Squad(2, unit7, Integer.parseInt(unitCount7.getValue()), Integer.parseInt(threshold7.getValue()));
                Squad squad8 = new Squad(3, unit8, Integer.parseInt(unitCount8.getValue()), Integer.parseInt(threshold8.getValue()));
                Squad squad9 = new Squad(4, unit9, Integer.parseInt(unitCount9.getValue()), Integer.parseInt(threshold9.getValue()));
                Squad squad10 = new Squad(5, unit10, Integer.parseInt(unitCount10.getValue()), Integer.parseInt(threshold10.getValue()));//info(squad1.toString());
*/
                List<Squad> squads1 = new ArrayList();
                List<Squad> squads2 = new ArrayList();

       /*         squads1.add(squad1);
                squads1.add(squad2);
                squads1.add(squad3);
                squads1.add(squad4);
                squads1.add(squad5);

                squads2.add(squad6);
                squads2.add(squad7);
                squads2.add(squad8);
                squads2.add(squad9);
                squads2.add(squad10);
*/
                army1.setSquads(squads1);
                army2.setSquads(squads2);

              


                if (army1.getSquadAtPosition(5).isActive()) {
                    info("Army 1 has won.");
                } else {
                    info("Army 2 has won.");
                }
                info(army1.toString());
                info(army2.toString());
            }
        };

        add(form);
        form.add(listUnits1); //first army
        form.add(listUnits2);
        form.add(listUnits3);
        form.add(listUnits4);
        form.add(listUnits5);

        form.add(unitCount1);
        form.add(unitCount2);
        form.add(unitCount3);
        form.add(unitCount4);
        form.add(unitCount5);

        form.add(threshold1);
        form.add(threshold2);
        form.add(threshold3);
        form.add(threshold4);
        form.add(threshold5);

        form.add(listUnits6); //second army
        form.add(listUnits7);
        form.add(listUnits8);
        form.add(listUnits9);
        form.add(listUnits10);

        form.add(unitCount6);
        form.add(unitCount7);
        form.add(unitCount8);
        form.add(unitCount9);
        form.add(unitCount10);

        form.add(threshold6);
        form.add(threshold7);
        form.add(threshold8);
        form.add(threshold9);
        form.add(threshold10);

    }
}
